// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Moving Spikes Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer


// ========================
// Function Declarations
// ========================

reserve function MovingSpikes_DebugDraw
reserve function MovingSpikes_DebugSpawn
reserve function MovingSpikes_TryPlayClankSfx


// ========================
// Function Definitions
// ========================

private function MovingSpikes_DebugDraw
	DrawSprite(0)
end function


private function MovingSpikes_DebugSpawn
	CreateTempObject(TypeName[Moving Spikes], 0, object.xpos, object.ypos)
	object[tempObjectPos].inkEffect = INK_BLEND
	object[tempObjectPos].drawOrder = 5
	if object.direction == FLIP_X
		object[tempObjectPos].xpos += 0x680000
		object[tempObjectPos].xvel = -0x10000
		object[tempObjectPos].timer = 104
	else
		object[tempObjectPos].xpos -= 0x680000
		object[tempObjectPos].xvel = 0x10000
		object[tempObjectPos].timer = -104
	end if
end function


private function MovingSpikes_TryPlayClankSfx
	temp0 = screen.xcenter
	temp0 += 32
	CheckCameraProximity(object.ixpos, object.iypos, temp0, 160)
	if checkResult == true
		PlaySfx(SfxName[Clank], false)
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	object.xpos += object.xvel
	if object.xvel > 0
		object.timer++
		if object.timer == 104
			FlipSign(object.xvel)
			CallFunction(MovingSpikes_TryPlayClankSfx)
		else
			foreach (TypeName[Moving Spikes], arrayPos0, ACTIVE_ENTITIES)
				if arrayPos0 != object.entityPos
					BoxCollisionTest(C_TOUCH, object.entityPos, 0, -40, 25, 40, arrayPos0, -25, -40, 0, 40)
					if checkResult == true
						FlipSign(object.xvel)
						CallFunction(MovingSpikes_TryPlayClankSfx)
					end if
				end if
			next
		end if
	else
		object.timer--
		if object.timer == -104
			FlipSign(object.xvel)
			CallFunction(MovingSpikes_TryPlayClankSfx)
		else
			foreach (TypeName[Moving Spikes], arrayPos0, ACTIVE_ENTITIES)
				if arrayPos0 != object.entityPos
					BoxCollisionTest(C_TOUCH, object.entityPos, -25, -40, 0, 40, arrayPos0, 0, -40, 25, 40)
					if checkResult == true
						FlipSign(object.xvel)
						CallFunction(MovingSpikes_TryPlayClankSfx)
					end if
				end if
			next
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -32, 16, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-24, -40, 48, 80, 206, 26)
	SpriteFrame(8, -16, 4, 32, 239, 107)

	foreach (TypeName[Moving Spikes], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_INACTIVE
		object[arrayPos0].inkEffect = INK_BLEND
		object[arrayPos0].drawOrder = 5
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].xpos += 0x680000
			object[arrayPos0].xvel = -0x10000
			object[arrayPos0].timer = 104
		else
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].xpos -= 0x680000
				object[arrayPos0].xvel = 0x10000
				object[arrayPos0].timer = -104
			else
				object[arrayPos0].xvel = -0x10000
			end if
		end if
	next

	SetTableValue(TypeName[Moving Spikes], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MovingSpikes_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MovingSpikes_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
		case 0 // type
			checkResult = object.propertyValue
			break
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
		case 0 // type
			object.propertyValue = editor.variableValue
			break
		end switch
	end if
end event


event RSDKDraw
	object.inkEffect = INK_BLEND
	
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-24, -40, 48, 80, 206, 26)
	SpriteFrame(8, -16, 4, 32, 239, 107)
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
	AddEnumVariable("Center", 2)
end event
